package com.common.frame.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

public class FormatUtils {

    private final static long minute = 60 * 1000;// 1分钟
    private final static long hour = 60 * minute;// 1小时
    private final static long day = 24 * hour;// 1天
    private final static long month = 31 * day;// 月
    private final static long year = 12 * month;// 年
    public final static String yyyy_MM_dd_HH_mm_ss =  "yyyy-MM-dd HH:mm:ss";
    public final static String yyyy_MM_dd_HH_mm =  "yyyy-MM-dd HH:mm";
    public final static String MM_dd_HH_mm =  "MM-dd HH:mm";

    private static HashMap<String, SimpleDateFormat> formatHashMap = new HashMap<>();

    public static SimpleDateFormat getSimpleDateFormat(String format){
        SimpleDateFormat dateFormat = formatHashMap.get(format);
        if (dateFormat == null){
            dateFormat = new SimpleDateFormat(format, Locale.SIMPLIFIED_CHINESE);
            formatHashMap.put(format, dateFormat);
        }
        return dateFormat;
    }

    public static String formatTime(long time, String format) {
        return getSimpleDateFormat(format).format(new Date(time));
    }

    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String milSecond){
        if (milSecond == null ||  milSecond.equals("")) {
            milSecond = System.currentTimeMillis() + "";
        }
        Date date = new Date(Long.parseLong(milSecond) * 1000);
        SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
        return format.format(date);
    }

    public static String getTimeFormatText(long milSecond) {
        if (milSecond == 0) {
            return "刚刚";
        }
        Date date = new Date(milSecond * 1000);
        long diff = new Date().getTime() - date.getTime();
        long r = 0;
        if (diff > year) {
            r = (diff / year);
            return formatTime(date.getTime(), MM_dd_HH_mm);
        }
        if (diff > month) {
            r = (diff / month);
            return formatTime(date.getTime(), MM_dd_HH_mm);
        }
        if (diff > day) {
            r = (diff / day);
            if (r > 5)
            {
                return formatTime(date.getTime(), MM_dd_HH_mm);
            }else {
                return r + "天前";
            }
        }
        if (diff > hour) {
            r = (diff / hour);
            return r + "个小时前";
        }
        if (diff > minute) {
            r = (diff / minute);
            return r + "分钟前";
        }
        return "刚刚";
    }

    public static String dealDuration(double duration){
        int temp = (int) duration;
        if (temp < 60){
            return "00:" + dealTempFormat(temp);
        }
        if (temp < 3600){
            return dealTempFormat(temp / 60) + ":" + dealTempFormat(temp%60);
        }
        return dealTempFormat(temp/3600) + ":" + dealTempFormat((temp%3600) / 60) + ":" + dealTempFormat(temp%60);
    }

    public static String dealTempFormat(int temp){
        if (temp < 10) return "0" + temp;
        return String.valueOf(temp);
    }


}
